
//类似这种QUERY_SQL语句 其实就是对应增删改查的SQL语句

//以下为实例，根据自身需求改动过

//查询
router.get('/search', async ctx => {
	const data = await query(QUERY_SQL);
	ctx.body = {
		data,
	};
});

// 插入
router.post('/save', async ctx => {
	const res = ctx.request.body;
	const { name = '', age = null } = res;
	if(name && age) {
		const queryData = {
			name,
			age,
		};
		const data = await query(INSERT_SQL, queryData);
		if(data && data.status && data.status === 200) {
			ctx.body = {
				status: 200,
				failed: false,
			};
		} else {
			ctx.body = data;
		}
	}
});

//更新
router.post('/update', async ctx => {
	const res = ctx.request.body;
	const { name = '', age = null, id } = res;
	if(name && age && id) {
		const queryData = [name, age, id];
		const data = await query(UPDATE_SQL, queryData);
		if(data && data.status && data.status === 200) {
			ctx.body = {
				status: 200,
				failed: false,
			};
		} else {
			ctx.body = data;
		}
	}
});

//根据主键id 删除
router.del('/delete', async ctx => {
	const res = ctx.request.body;
	const { id } = res;
	if(id) {
		const queryData = [id];
		const data = await query(DELETE_SQL, queryData);
		if(data && data.status && data.status === 200) {
			ctx.body = {
				status: 200,
				failed: false,
			};
		} else {
			ctx.body = data;
		}
	}
});

export default router;
